import cn from './lang/cn'
import en from './lang/en'
import { createI18n } from 'vue-i18n'
import { Locale } from 'vant'
import enUS from 'vant/es/locale/lang/en-US'

let locale = 'cn'
const browerlang = (navigator.language || navigator.browserLanguage).toLowerCase()
const cn_langs = ['zh-cn', 'zh-tw', 'zh-hk']
if(cn_langs.indexOf(browerlang) < 0 ){
    locale = 'en'
    Locale.use('en-US', enUS);
}else {
    locale = 'cn'
}

const messages = {
  'cn': cn,
  'en': en
}

//设置html语言
let h = document.querySelector('html');
h.setAttribute('lang', locale);


export const i18n = createI18n({
  locale: locale, // set locale
  fallbackLocale: 'cn', // set fallback locale
  messages // set locale messages
})

const regex = /(\{[\w]+\})/g

// 智能翻译简单语句，模板无法使用，可以扩展
export function selfI18n (message, data) {
  if(message===undefined)
  {
    return ''
  }
  const {t}=i18n.global;
  return t(message,data)
  // if (!data) {
  //   return messages[locale][message] || message
  // }

  // const resultmessage = messages[locale][message] || message

  // return resultmessage.replace(regex, (value) => {
  //   value = value.replace('{', '')
  //   value = value.replace('}', '')
  //   return data[value]
  // })
}
